package javacommon.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class NetUtils {
	public static boolean isNetFileAvailable(String strUrl) {
		InputStream netFileInputStream = null;
		try {
			URL url = new URL(strUrl);
			URLConnection urlConn = url.openConnection();
			netFileInputStream = urlConn.getInputStream();
			if (null != netFileInputStream) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			return false;
		} finally {
			try {
				if (netFileInputStream != null)
					netFileInputStream.close();
			} catch (IOException e) {
			}
		}
	}

	public static boolean isHttpAvailable(String urlStr) {
		if (urlStr == null || urlStr.length() <= 0) {
			return false;
		}
		try {
			URL url = new URL(urlStr);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			int state = con.getResponseCode();
			if (state == 200) {
//				System.out.println("URL可用！");
				if(con.getURL().equals(url)){//未跳转，还是原地址
					return true;
				}else {
					return false;
				}
			}
		} catch (Exception ex) {
			return false;
		}
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(isHttpAvailable("http://news.sohu.com/s2013/yaandizhen_gd/index_50.shtml"));
	}
}
